package com.example.demo.service;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.BookMapper;
import com.example.demo.dao.RoleMapper;
import com.example.demo.model.Book;
import com.example.demo.model.BookBySearch;
import com.example.demo.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/12/19 14:24
 */
@Service
public class SearchServiceMysql {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private RoleMapper roleMapper;

    public List<Book> search(String keyWord) {
        List<Book> books=new ArrayList<Book>();
        List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().select("rolename", "bookid"));
//        List<Book> booksbyname = bookMapper.selectList(new QueryWrapper<Book>().select("id","name","type","author"));
        for (Role role : roles) {
            /*Book book = booksbyname.get(role.getBookid());
            BookBySearch bookBySearch = new BookBySearch(book.getId(), book.getName(), book.getType(), book.getAuthor(), role.getRolename());*/
       if (role.getRolename().equals(keyWord))
       {
           System.out.println(role.getRolename());
           books.add(bookMapper.selectOne(new QueryWrapper<Book>().select("id","name","type","author").eq("id",role.getBookid())));
       }

        }
        books.addAll(bookMapper.selectList(new QueryWrapper<Book>().select("id","name","type","author").like("name", keyWord))) ;

return books;
    }
}